# -*- coding:utf-8 -*-
# created by jinss

# 1.确定 在文件中 存储的账号信息的结构

# 2.读取数据到内存中，为了方便，设计数据为字典类型
accounts = {}
f = open("account.db", "r")
for line in f:
    line = line.strip().split(",")
    accounts[line[0]] = line
print(accounts)

# 3.搞个loop，要求你用户输入账号信息，判断就可以
while True:
    username = input("Username:").strip()
    if username not in accounts:
        print("该用户未注册。。。")
        continue
    elif accounts[username][2] == "1":
        print("此账号已锁定，请联系管理员")
        continue
    count = 0
    while count < 3:
        password = input("Password:").strip()
        if password == accounts[username][1]:
            print(f"登录成功，Welcome {username}...")
            exit("bye...")
        else:
            print("Wrong password...")

        count += 1

    if count == 3:
        print(f"输错了{count}次密码，需要锁定账号{username}。。。")

        accounts[username][2] = "1"
        f2 = open("account.db", mode="w", encoding="utf-8")
        for user,val in accounts.items():
            line = ",".join(val) + "\n"
            f2.write(line)
        f2.close()

        exit("bye...")

